package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.utils.WriteableUtil;

public class ConfigGoGenerator {
	protected String fileName = "config.go";
	public ConfigGoGenerator(){
		super();
		this.fileName = "config.go";
	}

	public String generateString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package config"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(4000L,1,"\"github.com/spf13/viper\""));
		sList.add(new Statement(5000L,1,"\"time\""));
		sList.add(new Statement(6000L,1,"\"os\""));
		sList.add(new Statement(7000L,1,"\"log\""));
		sList.add(new Statement(8000L,0,")"));
		sList.add(new Statement(9000L,0,""));
		sList.add(new Statement(10000L,0,"func LogInfo()   {"));
		sList.add(new Statement(11000L,1,"file := \"./\" + time.Now().Format(\"2006-01-02\") + \".log\""));
		sList.add(new Statement(12000L,1,"logFile, _ := os.OpenFile(file,os.O_RDWR| os.O_CREATE| os.O_APPEND, 0766)"));
		sList.add(new Statement(13000L,1,"log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)"));
		sList.add(new Statement(14000L,1,"log.SetOutput(logFile)"));
		sList.add(new Statement(15000L,0,"}"));
		sList.add(new Statement(16000L,0,""));
		sList.add(new Statement(17000L,0,"func Init() error {"));
		sList.add(new Statement(18000L,1,"if err := Config(); err != nil {"));
		sList.add(new Statement(19000L,2,"return err"));
		sList.add(new Statement(20000L,1,"}"));
		sList.add(new Statement(21000L,1,"LogInfo()"));
		sList.add(new Statement(22000L,1,"return nil"));
		sList.add(new Statement(23000L,0,"}"));
		sList.add(new Statement(24000L,0,""));
		sList.add(new Statement(25000L,0,"func Config() error  {"));
		sList.add(new Statement(26000L,1,"viper.AddConfigPath(\"conf\")"));
		sList.add(new Statement(27000L,1,"viper.SetConfigName(\"config\")"));
		sList.add(new Statement(28000L,1,"if err := viper.ReadInConfig(); err != nil {"));
		sList.add(new Statement(29000L,2,"return err"));
		sList.add(new Statement(30000L,1,"}"));
		sList.add(new Statement(31000L,1,"return nil"));
		sList.add(new Statement(32000L,0,"}"));
		return WriteableUtil.merge(sList).getContent();
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
